package MyRedis

import (
	"fmt"
	"time"
)

const (
	ATTR_EXPIRE = "expr"
	ATTR_NX     = "nx"
	ATTR_XX     = "xx"
)

/**
OperationAttr 操作属性
*/
type OperationAttr struct {
	Name  string
	Value interface{}
}

//用于 ...OperationAttr 参数 转化
type OperationAttrs []*OperationAttr

/**
Find 查找操作属性
*/
func (this OperationAttrs) Find(name string) *InterfaceResult {
	for _, attr := range this {
		if attr.Name == name {
			return NewInterfaceResult(attr.Value, nil)
		}
	}
	return NewInterfaceResult(nil, fmt.Errorf("OperationAttrs found error:%s", name))
}

/**
WithExpire  过期时间属性
*/
func WithExpire(t time.Duration) *OperationAttr {
	return &OperationAttr{Name: ATTR_EXPIRE, Value: t}
}

/**
WithNx  NX命令: 仅当key不存在时，set才会生效
*/
func WithNx() *OperationAttr {
	return &OperationAttr{Name: ATTR_NX, Value: &struct {
	}{}}

}

/**
WithXx XX命令: 仅当key存在时，set才会生效
*/
func WithXx() *OperationAttr {
	return &OperationAttr{Name: ATTR_XX, Value: &struct {
	}{}}
}
